import java.util.HashMap;
import java.util.Map;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: 冯若航
 * \* Date: 2021/6/12
 * \* Time: 17:57
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class 无重复字符的最长子串 {
    public int lengthOfLongestSubstring(String s) {
        Map<Character,Integer> window=new HashMap<>();
        int left=0,right=0;
        int res=0;
        while (right<s.length()){
            char c=s.charAt(right);
            right++;
            window.put(c,window.getOrDefault(c,0)+1);
            //有重复字符了就左窗口往右滑动，直到没有重复字符为止
            while (window.get(c)>1){
                char d=s.charAt(left);
                left++;
                window.put(d,window.getOrDefault(d,0)-1);
            }
            res=Math.max(res,right-left);
        }
        return res;
    }
}